Component({
  options: {
    addGlobalClass: true
  },
  properties: {
    show: {
      type: Boolean,
      value: true,
    },
  },
  data: {
    timeList: -1,
    timeValue: "",
    selectText:"",
    startdate: '',//默认起始时间  
    enddate: '',//默认结束时间 
    // 提交日期选项
    submitDateList: [
      { text: '全部', value: 0 },
      { text: '近一周', value: 7 },
      { text: '近半月', value: 15 },
      { text: '近一月', value: 30 },
    ],
  },
  attached() {
    var _this = this;
    var dateNow = new Date();
    var year = dateNow.getFullYear();
    var month = dateNow.getMonth() + 1;
    if(month <10){
      month = "0" + month;
    }
    var day = dateNow.getDate()
    if(day <10){
      day = "0" + day;
    }
    var date = year + "-" + month + "-" + day;
    var today = date;
    _this.setData({
      startdate: date,
      enddate: date,
      today:today,
    });
  },
  methods: {
    //点击时间
    clickTime(e) {
      var _this = this;
      var toData = new Date();
      var year = toData.getFullYear();
      var month = toData.getMonth() + 1;
      var day = toData.getDate();
      _this.setData({
        selectAll:false,
        enddate:year + "-" + month + "-" + day
      });
      //最近7天
      let past7daysStart = _this.timestampToTime(toData - 7 * 3600 * 24 * 1000)
      //最近15天
      let past15daysStart = _this.timestampToTime(toData - 15 * 3600 * 24 * 1000)
      //最近30天
      let past30daysStart = _this.timestampToTime(toData - 30 * 3600 * 24 * 1000)
      _this.setData({
        timeList: e.currentTarget.dataset.index,
        timeValue: e.currentTarget.dataset.value.value,
      })

      if (_this.data.timeValue == 0) {
        _this.setData({
          selectAll:true,
          selectText:null,
        })
      }
      if (_this.data.timeValue == 30) {
        _this.setData({
          startdate: past30daysStart,
          selectText:e.currentTarget.dataset.value.text,
        })
      }
      if (_this.data.timeValue == 15) {
        _this.setData({
          startdate: past15daysStart,
          selectText:e.currentTarget.dataset.value.text,
        })
      }
      if (_this.data.timeValue == 7) {
        _this.setData({
          startdate: past7daysStart,
          selectText:e.currentTarget.dataset.value.text,
        })
      }
    },
    timestampToTime(timestamp) {
      var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var Y = date.getFullYear() + '-';
      var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
      var D = date.getDate() + ' ';
      return Y + M + D;
    },
    getDateWithoutYear(date){
      var dates = date.split('-');
      return dates[1] + "-" + dates[2];
    },
    //开始时间
    bindDateChangeStart(e) {
      let _this = this;
      _this.setData({
        startdate: e.detail.value,
        timeList: -1,
      })
      var selectText = _this.getDateWithoutYear(_this.data.startdate) + "~" +
      _this.getDateWithoutYear(_this.data.enddate);
      _this.setData({
        selectText:selectText,
      })
    },
    //结束时间
    bindDateChangeEnd(e) {
      let _this = this;
      _this.setData({
        enddate: e.detail.value,
        timeList: -1,
      })
      var selectText = _this.getDateWithoutYear(_this.data.startdate) + "~" +
      _this.getDateWithoutYear(_this.data.enddate);
      _this.setData({
        selectText:selectText,
      })
    },
    //选择时间点击确定
    bindTimeClick() {
      let _this = this;
      _this.setData({
        show: false
      })
      var startDate = _this.data.startdate;
      var startTime = _this.data.startdate + " 00:00:00";
      var endDate = _this.data.enddate;
      var endTime = _this.data.enddate + " 23:59:59";
      if(_this.data.selectAll){
        startDate = null;
        startTime = null;
        endDate = null;
        endTime = null;
      }
      _this.triggerEvent('confirm', { 
        text:_this.data.selectText,
        startDate:startDate,
        startTime: startTime,
        endDate:endDate,
        endTime: endTime,
      })
    },
  },
})